import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import { getToken } from '@/utils/localStorage.js'
import {Message} from "element-ui"
Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'Home',
    // component: Home,
    component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue'),
    children:[
      {
        path: '/admin',
        name: 'Admin',
        component: () => import(/* webpackChunkName: "about" */ '../views/admin/Admin.vue')
      },
      {
        path: '/user',
        name: 'User',
        component: () => import(/* webpackChunkName: "about" */ '../views/user/User.vue')
      },      
      {
        path: '/goods',
        name: 'Goods',
        component: () => import(/* webpackChunkName: "about" */ '../views/goods/Goods.vue')
      },
      {
        path: '/brand',
        name: 'Brand',
        component: () => import(/* webpackChunkName: "about" */ '../views/goods/Brand.vue')
      },      
      {
        path: '/type',
        name: 'Type',
        component: () => import(/* webpackChunkName: "about" */ '../views/goods/Type.vue')
      }, 
      {
        path:'/address',
        name:'Address',
        component:()=>import(/* webpackChunkName: "about" */'../views/orders/address.vue')
      },
      {
        path:'/order',
        name:'Order',
        component:()=>import(/* webpackChunkName: "about" */'../views/orders/order.vue')
      }     
    ]
  },
  {
    path: '/',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/") {
    next();
  } else {
    if (getToken() != null) {
      next();
    } else {
      router.push("/");
      Message.error("请登录！")
    }
  }
});

/**
 * 重写路由的push方法
 */
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => {
    console.log("路由添加重复"+error)
  })
}

export default router
